//
//  UIView+Extension.swift
//  Swift-Categary
//
//  Created by lantaiyuan on 2017/9/11.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import Foundation
import UIKit

let line_height : CGFloat = CGFloat(SEPARATOR_HEIGHT)

extension UIView {
    
    /// X
    public var x : CGFloat{
        
        get{
            return frame.origin.x
        }
        set(newValue){
            var tempValue : CGRect = frame
            tempValue.origin.x = newValue
            frame = tempValue
        }
    }
    
    /// Y
    public var y : CGFloat{
        get{
            return frame.origin.y
        }
        set(newValue){
            var tempValue : CGRect = frame
            tempValue.origin.y = newValue
            frame = tempValue
        }
        
    }
    
    /// height
    public var height : CGFloat{
        get{
            return frame.size.height
        }
        set(newValue){
            var tempValue : CGRect = frame
            tempValue.size.height = newValue
            frame = tempValue
        }
    }
    
    /// width
    public var width : CGFloat{
        get{
            return frame.size.width
        }
        set(newValue){
            var tempValue : CGRect = frame
            tempValue.size.width = newValue
            frame = tempValue
        }
    }
    
    /// size
    public var size : CGSize{
        get{
            return frame.size
        }
        set(newValue){
            var tempValue : CGRect = frame
            tempValue.size = newValue
            frame = tempValue
        }
    }
    
    /// origin
    public var origin: CGPoint{
        get{
            return self.frame.origin
        }
        set(newValue){
            
            x = newValue.x
            y = newValue.y
        }
    }
    
    /// centerX
    public var centerX : CGFloat{
        get{
            return center.x
        }
        set(newValue){
            var tempValue : CGPoint = center
            tempValue.x = newValue
            center = tempValue
        }
    }
    
    /// centerY
    public var centerY : CGFloat{
        get{
            return center.y
        }
        set(newValue){
            var tempValue : CGPoint = center
            tempValue.y = newValue
            center = tempValue
        }
    }
    
    
    /// 右边界的X值
    public var rightX: CGFloat{
        get{
            return x + width
        }
        set(newValue){
            var tempValue = self.frame
            tempValue.origin.x = newValue - frame.size.width
            frame = tempValue
        }
    }
    
    /// 下边界的Y值
    public var bottomY: CGFloat{
        get{
            return y + height
        }
        set(newValue){
            var tempValue = frame
            tempValue.origin.y = newValue - frame.size.height
            frame = tempValue
        }
    }
    
    public func drawBorder() {
        
        self.layer.borderColor = UIColor.init(white: 0.7, alpha: 0.5).cgColor
        self.layer.borderWidth = line_height
    }
    
    public func drawBorderWithColor(_ color:UIColor) {
        
        self.layer.borderColor = color.cgColor
        self.layer.borderWidth = line_height
    }
    
    public func drawBorderWithColor(_ color:UIColor,width:CGFloat) {
        self.layer.borderColor = color.cgColor
        self.layer.borderWidth = width
    }
    
    public func drawBorderWithCornerRadius(_ radius:CGFloat) {
        self.layer.cornerRadius = radius
    }
    public func drawBorderWithColorRadius(color:UIColor,radius:CGFloat) {
        self.layer.borderColor = color.cgColor
        self.layer.borderWidth = line_height
        self.layer.cornerRadius = radius
    }
    public func drawBorderWithColor(_ color:UIColor,width:CGFloat,radius:CGFloat) {
        self.layer.borderColor = color.cgColor
        self.layer.borderWidth = width
        self.layer.cornerRadius = radius
    }
    
    //添加阴影
    public func addShadow() {
        self.layer.shadowColor = UIColor.red.cgColor
        self.layer.shadowOffset = CGSize(width:10,height:1)
        self.layer.shadowOpacity = 2
        self.layer.shadowRadius = 2
    }
    
    public func addBottomShadow(_ color:UIColor) {
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOffset = CGSize(width:0,height:1)
        self.layer.shadowOpacity = 0.3
        self.layer.shadowRadius = 5.0
    }
    
    public func drawLineImageWithRect(_ rect:CGRect) {
        let line = UIImageView.init(frame: rect)
        line.image = UIImage.init(named: "separator")
        self.addSubview(line)
    }
    
    /* 划线*/
    public func drawLineWithColor(_ color:UIColor,rect:CGRect) {
        let lineLayer = CALayer()
        lineLayer.backgroundColor = color.cgColor
        lineLayer.frame = rect
        self.layer.addSublayer(lineLayer)
    }
    
    public func addTopLineWithColor(_ color:UIColor) {
        drawLineWithColor(color, rect: CGRect(x:0,y:0,width:self.width,height:self.height))
    }
    
    public func addTopLineWithColor(_ color:UIColor,width:CGFloat) {
        drawLineWithColor(color, rect: CGRect(x:0,y:0,width:width,height:line_height))
    }
    
    public func addBottomLineWithColor(_ color:UIColor) {
        drawLineWithColor(color, rect: CGRect(x:0,y:self.height - line_height,width:self.width,height:line_height))
    }
    public func addBottomLineWithColor(_ color:UIColor,height:CGFloat) {
        drawLineWithColor(color, rect: CGRect(x:0,y:self.height - height,width:self.width,height:height))
    }
    
    public func addRightLineWithColor(_ color:UIColor) {
        drawLineWithColor(color, rect: CGRect(x:self.width - line_height,y:0,width:line_height,height:self.height))
    }
    
    public  func addRightLineWithColor(_ color:UIColor,width:CGFloat) {
        drawLineWithColor(color, rect: CGRect(x:self.width - width,y:0,width:width,height:self.height))
    }
    
    public func hideKeyboard() {
        
        UIApplication.shared.keyWindow?.endEditing(true)
    }
    
    public func getFirstResponder() ->UIView {
        
        if self.isFirstResponder {
            return self
        }
        
        for subView in self.subviews {
            let firstResponder = subView.getFirstResponder() as UIView
            
            return firstResponder
            
        }
        return UIView()
    }
    
    //MARK  ---扩展方法
    public func viewController() -> UIViewController? {
        //通过响应链, 取得视图所在的控制器
        var next = self.next
        
        repeat {
            //判断响应者对象是否是视图控制器类型
            if (next?.isKind(of: UIViewController.classForCoder()))! {
                return next as? UIViewController
            }
            next = next?.next
        }while next != nil
        return nil
    }
    //获取当前控制器
    static func topViewController() -> UIViewController {
        var resultVC:UIViewController
        resultVC = self._topViewController(vc: (UIApplication.shared.keyWindow?.rootViewController)!)
        while (resultVC.presentedViewController != nil) {
            resultVC = self._topViewController(vc: resultVC.presentedViewController!)
        }
        return resultVC
    }
    
    static func _topViewController(vc:UIViewController) -> UIViewController {
        
        if vc.isKind(of:UINavigationController.self) {
            return self._topViewController(vc: (vc as? UINavigationController)!.topViewController!)
        } else if vc.isKind(of: UITabBarController.self){
            return self._topViewController(vc: (vc as? UITabBarController)!.selectedViewController!)
        }else {
            return vc
        }
    }
    //裁剪view的圆角
    func clipRectCorner(direction : UIRectCorner,cornerRadius : CGFloat) {
        
        let cornerSize = CGSize(width : cornerRadius,height : cornerRadius)
        let maskPath = UIBezierPath(roundedRect:bounds,byRoundingCorners : direction,cornerRadii:cornerSize)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = maskPath.cgPath
        layer.addSublayer(maskLayer)
        layer.mask = maskLayer
    }

    /**
     *  设置部分圆角(绝对布局)
     *
     *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
     *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
     */
    public func addRoundedCorners(corners:UIRectCorner,radii:CGSize) {
        let rounded:UIBezierPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: radii)
        let shape:CAShapeLayer = CAShapeLayer.init()
        shape.path = rounded.cgPath
        self.layer.mask = shape
    }
    
    /**
     *  设置部分圆角(相对布局)
     *
     *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
     *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
     *  @param rect    需要设置的圆角view的rect
     */
    
    public func addRoundedCornersWithRadiiviewRect(corners:UIRectCorner,radii:CGSize,rect:CGRect) {
        let rounded:UIBezierPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: radii)
        let shape:CAShapeLayer = CAShapeLayer.init()
        shape.path = rounded.cgPath
        self.layer.mask = shape
    }
    
    //设置支付方式表头
    public func sectionHeader() -> UIView {
        let view = UIView.init(frame: CGRect(x:0,y:0,width:SCREEN_WIDTH,height:44))
        view.backgroundColor = CONTENT_BG_COLOR
        let label = UILabel.init(frame: CGRect(x:15,y:0,width:SCREEN_WIDTH - 15,height:view.height))
        label.text = "支付方式"
        label.textColor = SUB_TITLE_COLOR
        label.font =  UIFont.fontOfSize(15)
        view.addSubview(label)
        return view
    }
}
